package sea.simulate;

import sea.util.PublicParam;
import sea.util.SimHyAHelper;
import sea.util.WinHelper;

/**
 *
 * This class builds the layout of Statistics interface of Save
 * 
 * @author  Zhirun Ding
 * 
 * Created on 04/07/2012
 * 
 */
public class SimulateSStatisticsForm extends javax.swing.JDialog {
    
    /** Creates new form SimulateSStatisticsForm */
    public SimulateSStatisticsForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        timeButtonGroup = new javax.swing.ButtonGroup();
        statTypeButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        trajectoryRadioButton = new javax.swing.JRadioButton();
        finalstateRadioButton = new javax.swing.JRadioButton();
        timeRadioButton = new javax.swing.JRadioButton();
        timeText = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        averageRadioButton = new javax.swing.JRadioButton();
        varianceRadioButton = new javax.swing.JRadioButton();
        stddevRadioButton = new javax.swing.JRadioButton();
        stderrRadioButton = new javax.swing.JRadioButton();
        cvRadioButton = new javax.swing.JRadioButton();
        fanoRadioButton = new javax.swing.JRadioButton();
        skewRadioButton = new javax.swing.JRadioButton();
        kurtosisRadioButton = new javax.swing.JRadioButton();
        backButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        helpButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sea.SimHyAGui.class).getContext().getResourceMap(SimulateSStatisticsForm.class);
        setTitle(resourceMap.getString("SimulateSStatisticsForm.title")); // NOI18N
        setName("SimulateSStatisticsForm"); // NOI18N
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName("jPanel1"); // NOI18N

        timeButtonGroup.add(trajectoryRadioButton);
        trajectoryRadioButton.setSelected(true);
        trajectoryRadioButton.setText(resourceMap.getString("trajectoryRadioButton.text")); // NOI18N
        trajectoryRadioButton.setName("trajectoryRadioButton"); // NOI18N

        timeButtonGroup.add(finalstateRadioButton);
        finalstateRadioButton.setText(resourceMap.getString("finalstateRadioButton.text")); // NOI18N
        finalstateRadioButton.setName("finalstateRadioButton"); // NOI18N

        timeButtonGroup.add(timeRadioButton);
        timeRadioButton.setText(resourceMap.getString("timeRadioButton.text")); // NOI18N
        timeRadioButton.setName("timeRadioButton"); // NOI18N
        timeRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                timeRadioButtonItemStateChanged(evt);
            }
        });

        timeText.setText(resourceMap.getString("timeText.text")); // NOI18N
        timeText.setEnabled(false);
        timeText.setName("timeText"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(timeRadioButton)
                        .addGap(30, 30, 30)
                        .addComponent(timeText, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(finalstateRadioButton)
                    .addComponent(trajectoryRadioButton))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(trajectoryRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(finalstateRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timeRadioButton)
                    .addComponent(timeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18))
        );

        trajectoryRadioButton.getAccessibleContext().setAccessibleName(resourceMap.getString("jRadioButton1.AccessibleContext.accessibleName")); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        statTypeButtonGroup.add(averageRadioButton);
        averageRadioButton.setSelected(true);
        averageRadioButton.setText(resourceMap.getString("averageRadioButton.text")); // NOI18N
        averageRadioButton.setName("averageRadioButton"); // NOI18N

        statTypeButtonGroup.add(varianceRadioButton);
        varianceRadioButton.setText(resourceMap.getString("varianceRadioButton.text")); // NOI18N
        varianceRadioButton.setName("varianceRadioButton"); // NOI18N

        statTypeButtonGroup.add(stddevRadioButton);
        stddevRadioButton.setText(resourceMap.getString("stddevRadioButton.text")); // NOI18N
        stddevRadioButton.setName("stddevRadioButton"); // NOI18N

        statTypeButtonGroup.add(stderrRadioButton);
        stderrRadioButton.setText(resourceMap.getString("stderrRadioButton.text")); // NOI18N
        stderrRadioButton.setName("stderrRadioButton"); // NOI18N

        statTypeButtonGroup.add(cvRadioButton);
        cvRadioButton.setText(resourceMap.getString("cvRadioButton.text")); // NOI18N
        cvRadioButton.setName("cvRadioButton"); // NOI18N

        statTypeButtonGroup.add(fanoRadioButton);
        fanoRadioButton.setText(resourceMap.getString("fanoRadioButton.text")); // NOI18N
        fanoRadioButton.setName("fanoRadioButton"); // NOI18N

        statTypeButtonGroup.add(skewRadioButton);
        skewRadioButton.setText(resourceMap.getString("skewRadioButton.text")); // NOI18N
        skewRadioButton.setName("skewRadioButton"); // NOI18N

        statTypeButtonGroup.add(kurtosisRadioButton);
        kurtosisRadioButton.setText(resourceMap.getString("kurtosisRadioButton.text")); // NOI18N
        kurtosisRadioButton.setName("kurtosisRadioButton"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(averageRadioButton)
                            .addComponent(stddevRadioButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(stderrRadioButton)
                            .addComponent(varianceRadioButton)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cvRadioButton)
                            .addComponent(skewRadioButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(kurtosisRadioButton)
                            .addComponent(fanoRadioButton))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(averageRadioButton)
                    .addComponent(varianceRadioButton))
                .addGap(5, 5, 5)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stddevRadioButton)
                    .addComponent(stderrRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cvRadioButton)
                    .addComponent(fanoRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(skewRadioButton)
                    .addComponent(kurtosisRadioButton))
                .addGap(108, 108, 108))
        );

        backButton.setText(resourceMap.getString("backButton.text")); // NOI18N
        backButton.setName("backButton"); // NOI18N
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        nextButton.setText(resourceMap.getString("nextButton.text")); // NOI18N
        nextButton.setName("nextButton"); // NOI18N
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        helpButton.setText(resourceMap.getString("helpButton.text")); // NOI18N
        helpButton.setName("helpButton"); // NOI18N
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nextButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(helpButton)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(backButton)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nextButton)
                            .addComponent(helpButton))
                        .addGap(28, 28, 28)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        this.setVisible(false);
        
        if (PublicParam.PreviousForm != null) {
            PublicParam.PreviousForm.setVisible(true);
        }   
    }//GEN-LAST:event_backButtonActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        if (PublicParam.PreviousForm != null) {
            PublicParam.PreviousForm.setVisible(false);
        }
    }//GEN-LAST:event_formComponentShown

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        //run command of save first
        //generate plot command expression
        toSaveCommand();      
        //execute command
        this.setVisible(false);
        SimHyAHelper.executeCommand();        
        //close this form
        this.dispose();        
        //clear all form of plot
        WinHelper.closeFormAfterCommand();
    }//GEN-LAST:event_nextButtonActionPerformed

    private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
        WinHelper.showHelp("save/statistics");
    }//GEN-LAST:event_helpButtonActionPerformed

    private void timeRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_timeRadioButtonItemStateChanged
        timeText.setEnabled(timeRadioButton.isSelected());
    }//GEN-LAST:event_timeRadioButtonItemStateChanged
    
    void toSaveCommand() {
        String s = "";
        
        //stat type
        if (averageRadioButton.isSelected()) {
            s += ",stat=average";
        } else if (varianceRadioButton.isSelected()) {
            s += ",stat=variance";
        } else if (stddevRadioButton.isSelected()) {
            s += ",stat=stddev";
        } else if (stderrRadioButton.isSelected()) {
            s += ",stat=stderr";
        } else if (cvRadioButton.isSelected()) {
            s += ",stat=cv";
        } else if (fanoRadioButton.isSelected()) {
            s += ",stat=fano";
        } else if (skewRadioButton.isSelected()) {
            s += ",stat=skew";
        } else if (kurtosisRadioButton.isSelected()) {
            s += ",stat=kurtosis";
        }
        
        //time
        if (finalstateRadioButton.isSelected()) {
            s += ",time=final";
        } else if (timeRadioButton.isSelected() && timeText.getText().length() > 0) {
            s += ",time=" + timeText.getText();
        }  
        
        PublicParam.SimHyACommandExpression += s + ")";
        PublicParam.CommandTypeName = "Save"; 
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SimulateSStatisticsForm dialog = new SimulateSStatisticsForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton averageRadioButton;
    private javax.swing.JButton backButton;
    private javax.swing.JRadioButton cvRadioButton;
    private javax.swing.JRadioButton fanoRadioButton;
    private javax.swing.JRadioButton finalstateRadioButton;
    private javax.swing.JButton helpButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton kurtosisRadioButton;
    private javax.swing.JButton nextButton;
    private javax.swing.JRadioButton skewRadioButton;
    private javax.swing.ButtonGroup statTypeButtonGroup;
    private javax.swing.JRadioButton stddevRadioButton;
    private javax.swing.JRadioButton stderrRadioButton;
    private javax.swing.ButtonGroup timeButtonGroup;
    private javax.swing.JRadioButton timeRadioButton;
    private javax.swing.JTextField timeText;
    private javax.swing.JRadioButton trajectoryRadioButton;
    private javax.swing.JRadioButton varianceRadioButton;
    // End of variables declaration//GEN-END:variables
    
}
